const app = getApp();
var sourceType = [ ['camera'], ['album'], ['camera', 'album'] ];
var sizeType = [ ['compressed'], ['original'], ['compressed', 'original'] ];
Page({

// UID：登录人的用户ID ,ProductOne:一级分类ID,ProductType:二级分类ID,RecArea:范围1全国2全省3全市4全区县,RecPrice:价格,RecPriceUnit:价格单位,Number:数量,NumberUnit:数量单位,Mode:回收方式,IsTelInquiry:是否接收电话询问1是0否,Photo:产品图片（|线隔开）,Quality:质量要求,TransDesc:交易描述

  /**
   * 页面的初始数据
   */
  data: {
    Title:'',
    RecType:[],
    ProductOne:-1,
    ProductOneidx:0,
    ProductOneName:'请选择',
    ProductTypeArr:[],
    ProductType:-1,
    ProductTypeidx:0,
    ProductTypeName:'',
    ProductThreeArr:[],
    ProductThreeidx:0,
    ProductThree:-1,
    ProductThreeName:'',
    className:'请选择回收种类',
    selectS:false,
    animationData:{}, 
    tit_step:1,  
    RecAreaArr:[{name:'全国',code:1},{name:'全省',code:2},{name:'全市',code:3},{name:'全区县',code:4}],
    RecArea:0,
    RecAreaidx:0,
    RecAreatishi:'请选择回收区域',
    ModeArr:[{name:'可提供上门回收',code:1},{name:'只接受送货上门',code:2}],
    ModeIdx:1,
    Mode:0,
    Modetishi:'请选择回收类型',
    UnitArr:'',
    RecPrice:'',
    RecPriceUnit:'',
    RecPriceUnitIdx:0,
    RecPriceUnitTit:'单位',
    Number:'',
    NumberUnit:'',    
    NumberUnitIdx:0,    
    NumberUnitTit:'单位',    
    IsTelInquiry:0,
    Photo:[],
    Quality:'',
    TransDesc:''
  },
  addJbxx:function () {
    if (app.checkFjLogin()) {
      var FjLoginLog = wx.getStorageSync('FjLoginLog'); 
      if (this.data.Title == '') {
        app.tishi('请输入标题!');return false;
      }      
      if (this.data.ProductOne == -1 || this.data.ProductType == -1 || this.data.ProductThree == -1) {
        app.tishi('请选择回收种类!');return false;
      }
      if (this.data.RecArea == 0) {
        app.tishi('请选择回收区域!');return false;
      }
      var regNum=new RegExp('[0-9]','g');
      if (!regNum.exec(this.data.RecPrice)) {
        app.tishi('请输入回收价格!');return false;
      } 
      if (this.data.RecPriceUnit == '') {
        app.tishi('请选择回收价格单位!');return false;
      }
      if (!regNum.exec(this.data.Number)) {
        app.tishi('请输入回收数量!');return false;
      } 
      if (this.data.NumberUnit == '') {
        app.tishi('请选择数量单位!');return false;
      }
      if (this.data.Mode == 0) {
        app.tishi('请选择数回收类型!');return false;
      }
      if (this.data.Photo.length > 0) {
        var Photo = this.data.Photo.join('|'); 
      }else{
        var Photo = '';
      }
      var api_input = {
        UID:FjLoginLog.UID,
        ProductOne:this.data.ProductOne,
        ProductType:this.data.ProductType,
        ProductThree:this.data.ProductThree,
        RecArea:this.data.RecArea,
        RecPrice:this.data.RecPrice,
        RecPriceUnit:this.data.RecPriceUnit,
        Number:this.data.Number,
        NumberUnit:this.data.NumberUnit,
        Mode:this.data.Mode,
        IsTelInquiry:this.data.IsTelInquiry,
        Photo:Photo,
        Quality:this.data.Quality,
        TransDesc:this.data.TransDesc,
        Title:this.data.Title
      }
      var that = this;
      wx.showModal({
        title: '提示',
        content: '尊敬的客户您好,你现在正在添加回收分类,小程序正在进一步完善中,如果需要修改您填加的分类信息,请下载Feijiu网App,在app上进行修改',
        showCancel: false,
        confirmText:"我知道了",
        confirmColor: '#2BB56F',
        success: function (sm) {
          if (sm.confirm) {
            app.getDaoPostN('api/FJDispose/FJ_Add_RecType',api_input,r=>{
             
              if (r.code==200) {
                app.tishi('您的回收种类填加成功!');
                setTimeout(function () {
                  wx.navigateBack()
                },2000)
              }else{
                app.tishi(r.data);
              }
            }) 
          } 
        }
      }) 
    }else{
      wx.navigateTo({ url: '../../pages/login/index' }); 
    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.FJ_Get_Unit();
  },
  switchChange: function (e){
    if (e.detail.valu) {
      this.setData({
        IsTelInquiry:1
      })
    }else{
      this.setData({
        IsTelInquiry:0
      })      
    }
  }, 
  NumberUnit:function (e) {
    this.setData({
      NumberUnitTit:this.data.UnitArr[e.detail.value],
      NumberUnit:this.data.UnitArr[e.detail.value],
      NumberUnitIdx:e.detail.value
    })  
  },  
  RecPriceUnit:function (e) {
    this.setData({
      RecPriceUnit:this.data.UnitArr[e.detail.value],
      RecPriceUnitTit:this.data.UnitArr[e.detail.value],
      RecPriceUnitIdx:e.detail.value
    })  
  },   
  bindModeArr:function (e) {
    this.setData({
      Mode: this.data.ModeArr[e.detail.value].code,
      ModeIdx:e.detail.value,
      Modetishi:this.data.ModeArr[e.detail.value].name
    })    
  },
  bindFenLeiChange: function(e) {
    this.setData({
      RecArea: this.data.RecAreaArr[e.detail.value].code,
      RecAreaIdx:e.detail.value,
      RecAreatishi:this.data.RecAreaArr[e.detail.value].name
    })
  },  
  setpSet:function (e) {
    this.setData({
      tit_step:e.currentTarget.dataset.step
    })
  },
  selectP:function (e) {
    var arr = this.data.RecType[e.currentTarget.dataset.idx].list;
    arr.splice(0, 1);
    this.setData({
      tit_step:2,
      ProductTypeName:'请选择',
      ProductOneName:e.currentTarget.dataset.name,
      ProductOne:e.currentTarget.dataset.id,
      ProductOneidx:e.currentTarget.dataset.idx,
      ProductTypeArr:arr,
      ProductThreeName:''
    })
  },
  selectC:function (e) {
    this.setData({
      tit_step:3,
      ProductThreeName:'请选择',
      ProductTypeName:e.currentTarget.dataset.name,
      ProductType:e.currentTarget.dataset.id,
      ProductTypeidx:e.currentTarget.dataset.idx,
    })
    var api_input={
      lev:3,
      pid:e.currentTarget.dataset.id
    }
    var that = this;
    app.getDaoPostN('api/FJDispose/FJ_Get_RecTypeByID_New',api_input,function (r) {      
      if (r.code == 200) {
        that.setData({
          ProductThreeArr:r.data        
        })
      }
    })    
  }, 
  selectA:function (e) {
    var className = this.data.ProductOneName + '-' + this.data.ProductTypeName + '-' + e.currentTarget.dataset.name;
    this.setData({
      ProductThreeName:e.currentTarget.dataset.name,
      ProductThree:e.currentTarget.dataset.id,
      ProductThreeidx:e.currentTarget.dataset.idx,
      className:className
    })
    this.selectH();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getFJ_Get_RecType();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getFJ_Get_RecType:function () {
    var that = this;
    var timestamp = Date.parse(new Date());
    var storageRecType = wx.getStorageSync('storageRecType');
    var storageRecType_tamp = wx.getStorageSync('storageRecType_tamp');
    if (storageRecType && storageRecType_tamp > timestamp ) {
      storageRecType.splice(0, 1);
      this.setData({
        RecType:storageRecType
      })
    }else{
      app.getDaoPostN('api/FJDispose/FJ_Get_RecType_New','{}',function (r) {
       
        if (r.code == 200) {
          wx.setStorageSync('storageRecType',r.data);
          wx.setStorageSync('storageRecType_tamp',timestamp+604800000);
          r.data.splice(0, 1)
          that.setData({
            RecType:r.data
          })
        }
      })      
    }
  },  
  Title:function (e) {
    this.setData({
      Title:e.detail.value
    })    
  },
  RecPrice:function (e) {
    this.setData({
      RecPrice:e.detail.value
    })    
  },
  Quality:function (e) {
    this.setData({
      Quality:e.detail.value
    })    
  },    
  TransDesc:function (e) {
    this.setData({
      TransDesc:e.detail.value
    })  
  },
  Number:function (e) {
    this.setData({
      Number:e.detail.value
    })  
  },  
  selectH:function () {
    var that=this; 
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function(){
      if (that.data.ProductThree == -1) {
        that.setData({
          ProductOne:-1,
          ProductOneidx:0,
          ProductOneName:'请选择',
          ProductType:-1,
          ProductTypeidx:0,
          ProductTypeName:'',
          ProductThreeidx:-1,
          ProductThree:-1,
          ProductThreeName:'',
          className:'请选择回收种类',         
        })          
      }else{
        that.setData({
          selectS:false,          
        })
      }
    
    },200) 
  }, 
  selectS:function () {
    this.setData({
      selectS: true,  
    })
  },
  fadeIn:function(){
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })    
  },
  fadeDown:function(){
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),  
    })
  }, 
  zhuzhimaopao:function () {
    // body...
  }, 
  uploadImg:function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    wx.chooseImage({
      sourceType: sourceType[2],
      sizeType: sizeType[2],
      count: 1,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths[0];
        wx.showLoading({
          title: '图片上传中',
        })
        wx.uploadFile({
          url: app.data.api_url+'api/FJDispose/FJ_Upload_Img?Type=huishou&strrsa='+app.uploadImgStr(), 
          filePath: tempFilePaths,
          name: 'file',
          success (r){
            var rs = JSON.parse(r.data);
            if (rs.code == 200) {
             
              var ss = that.data.Photo;
              ss.push(rs.data);
              that.setData({
                Photo:ss
              })
              wx.hideLoading();
            } 
          },
          fail(r){
            wx.hideLoading();
            app.tishi('图片上传失败,请检查网络');
          }
        })        
      },
      fail(r){
        app.tishi('图片上传失败,请检查网络');
      }      
    })
  },
  imageDel:function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除本张图片吗？',
      success: function (sm) {
        if (sm.confirm) {
            var ind = e.currentTarget.dataset.ind;
            var list = that.data.Photo;
            list.splice(ind,1)
            that.setData({
              Photo:list
            })
        } 
      }
    })
  },   
  FJ_Get_Unit:function () {
    var that = this;
    app.getDaoPostN('api/FJDispose/FJ_Get_Unit','{}',function (r) {
      if (r.code == 200) {
        that.setData({
          UnitArr:r.data          
        })
      }
    }) 
  },  
})